package com.lzgcoding.onlineshopapp.feature.main.cart

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.lzgcoding.onlineshopapp.feature.main.cart.component.CartList
import com.lzgcoding.onlineshopapp.feature.main.cart.component.TotalCard
import com.lzgcoding.onlineshopapp.feature.main.dashboard.component.SearchField
import com.lzgcoding.onlineshopapp.ui.theme.MediumPadding
import com.lzgcoding.onlineshopapp.ui.theme.OnlineShopAppTheme
import org.koin.androidx.compose.koinViewModel

@Composable
fun CartScreen() {
    val viewModel = koinViewModel<CartViewModel>()
    val uiState by viewModel.uiState.collectAsStateWithLifecycle()
    Scaffold(
        topBar = {
            SearchField(
                modifier = Modifier
                    .fillMaxWidth()
                    .statusBarsPadding()
                    .padding(horizontal = MediumPadding),
                value = uiState.searchValue,
                onValueChange = viewModel::onSearchValueChange
            )
        }
    ) { innerPadding ->
        Box(
            modifier = Modifier
                .fillMaxSize()
                .padding(innerPadding)
        ) {
            CartList(
                items = uiState.items,
                onIncrease = viewModel::increase,
                onDecrease = viewModel::decrease
            )
            TotalCard(
                subtotal = uiState.subtotal,
                freeDelivery = uiState.freeDelivery,
                totalTax = uiState.totalTax,
                total = uiState.total
            )
        }
    }
}

@Preview(showBackground = true, showSystemUi = true)
@Composable
private fun CartScreenPreview() {
    OnlineShopAppTheme {
        CartScreen()
    }
}